package lion.dev.web.paging;

import lion.dev.lang.MapJ;

/**
 *
 * 有限行数的分页
 *
 * 处理了尾页边界的情况。
 *
 * @author lion
 * @date 2011-5-3 约定： 页数从1开始,行数从1开始
 */
public class LimitedPaging implements Paging {

	public static final int DEFAULT_PAGING_SIZE = 10;

	private int maxRowNumber = 0;
	private int pageNumber = 1;
	private int pageSize = 10;

	public LimitedPaging(int maxRowNumber) {

		if (maxRowNumber > -1) {
			this.maxRowNumber = maxRowNumber;
		}
		this.pageSize = DEFAULT_PAGING_SIZE;
	}

	@Override
	public void setPageNumber(int pageNumber) {

		if (this.pageNumber > 0) {
			this.pageNumber = pageNumber;
		}
	}

	@Override
	public void setPageSize(int pageSize) {

		if (this.pageSize > -1) {
			this.pageSize = pageSize;
		}
	}

	@Override
	public int getStartRowNumber() {

		return (this.pageNumber - 1) * this.pageSize + 1;
	}

	@Override
	public int getEndRowNumber() {

		int result = this.getStartRowNumber() + this.pageSize - 1;
		if (result > this.maxRowNumber) {
			result = this.maxRowNumber;
		}

		return result;
	}

	@Override
	public int getPageNumber() {

		return this.pageNumber;
	}

	@Override
	public int getPageCount() {

		return this.maxRowNumber % this.pageSize == 0 ? (this.maxRowNumber / this.pageSize) : (this.maxRowNumber / this.pageSize + 1);
	}

	@Override
	public boolean hasPreviousPaging() {

		return this.pageNumber > 1;
	}

	@Override
	public boolean hasNextPaging() {

		return this.pageNumber < this.getPageCount();
	}

	@Override
	public LimitedPaging getNextPaging() {

		if (!hasNextPaging()) { return null; }

		LimitedPaging limitedPaging = new LimitedPaging(maxRowNumber);
		limitedPaging.setPageNumber(pageNumber + 1);
		limitedPaging.setPageSize(pageSize);
		return limitedPaging;
	}

	@Override
	public LimitedPaging getPreviousPaging() {

		if (!hasPreviousPaging()) { return null; }
		LimitedPaging limitedPaging = new LimitedPaging(maxRowNumber);
		limitedPaging.setPageNumber(pageNumber - 1);
		limitedPaging.setPageSize(pageSize);
		return limitedPaging;
	}

	@Override
	public LimitedPaging getFirstPaging() {

		LimitedPaging limitedPaging = new LimitedPaging(maxRowNumber);
		limitedPaging.setPageNumber(1);
		limitedPaging.setPageSize(pageSize);
		return limitedPaging;
	}

	@Override
	public LimitedPaging getEndPaging() {

		LimitedPaging limitedPaging = new LimitedPaging(maxRowNumber);

		limitedPaging.setPageNumber(this.getPageCount());
		limitedPaging.setPageSize(pageSize);

		return limitedPaging;
	}

	@Override
	public int getMaxRowNumber() {

		return maxRowNumber;
	}

	@Override
	public int getPageSize() {

		return pageSize;
	}

	@Override
	public MapJ map() {

		MapJ result = new MapJ();
		result.put("pageCount", this.getPageCount());
		result.put("total", this.getMaxRowNumber());
		result.put("start", this.getStartRowNumber());
		result.put("pageSize", this.getPageSize());
		result.put("end", this.getEndRowNumber());
		result.put("page", this.getPageNumber());
		result.put("hasNext", this.hasNextPaging());
		result.put("hasPre", this.hasPreviousPaging());

		return result;
	}

	public static void main(String[] args) {

		LimitedPaging limitedPaging = new LimitedPaging(-1);
		limitedPaging.setPageNumber(2);
		limitedPaging.setPageSize(10);
		System.out.println(limitedPaging.getStartRowNumber() + "--" + limitedPaging.getEndRowNumber());
	}
}
